import { Component, OnInit, Input } from '@angular/core';
import { CloseWindowService } from './close-window.service';
import { AppdataService } from '../../services/appdata.service';
import { Router, } from '@angular/router';
declare var $: any;
@Component({
  selector: 'app-close-window',
  templateUrl: './close-window.component.html',
  styleUrls: ['./close-window.component.scss']
})
export class CloseWindowComponent implements OnInit {
  @Input('current') current:number;
  public modal:boolean;
  constructor(
    public closeWindowService: CloseWindowService,
    public appService: AppdataService,
    public router: Router,
  ) { }

  ngOnInit() {
    window.onbeforeunload = () => {
      this.closeWindow()
    };
  }

  exitStudy() {
    this.modal = true;
  }

  reStudy() {
    this.modal = false;
  }



  closeWindow() {
    this.appService.suspendData.router = this.router.routerState.snapshot.url;
    this.appService.suspendData.currentPage = this.current;
    this.router.navigate(['/home']);

    $.xcj.scorm.api.setSuspendData(JSON.stringify(this.appService.suspendData));
    $.xcj.scorm.api.doCommit();
    // 平台退出
    $.xcj.scorm.api.doQuit();
    $.xcj.scorm.api.doCommit();

    this.closeWindowService.closeWindow();
  }

}
